package com.jemmyee.pizza.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.jemmyee.core.dao.Page;
import com.jemmyee.pizza.entity.Message;
import com.jemmyee.pizza.service.IMessageService;
import com.jemmyee.pizza.web.Constants;
import com.jemmyee.pizza.web.bean.MessageBean;
import com.jemmyee.pizza.web.utils.SendMessageUtils;

/**
 * @author qiuchao
 * 2011-2-28
 */
@Controller
@RequestMapping("/admin/message")
public class MessageController {
	@Autowired
	private IMessageService messageService;
	
	@RequestMapping("/list/{from}/{startIndex}")
	public ModelAndView list(@PathVariable Integer startIndex,@PathVariable String from,@ModelAttribute MessageBean bean,HttpServletRequest request) {
		DetachedCriteria dc = DetachedCriteria.forClass(Message.class);
		MessageBean messageBean = new MessageBean();
		ModelAndView mv = new ModelAndView();
		if (from.equals("form")) {
			messageBean = bean;
			request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, messageBean);
		}
		if (from.equals("page")) {
			if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
				messageBean = (MessageBean) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			}
		}
		if (from.equals("menu")) {
			System.out.println("menu is call");
		}
		if(messageBean.getName() != null && !"".equals(messageBean.getName().trim())){
			dc.add(Restrictions.like("name", messageBean.getName(), MatchMode.ANYWHERE));
		}
		if(messageBean.getPhone() != null && !"".equals(messageBean.getPhone().trim())){
			dc.add(Restrictions.like("phone", messageBean.getPhone(), MatchMode.ANYWHERE));
		}
		if(messageBean.getStartTime() != null && !messageBean.getStartTime().equals("")) {
			dc.add(Restrictions.ge("sendTime", messageBean.getStartTime()));
		}
		if(messageBean.getEndTime() != null && !messageBean.getEndTime().equals("")) {
			dc.add(Restrictions.le("sendTime", messageBean.getEndTime()));
		}
		Page page = messageService.findPageByCriteria(dc, Constants.DEFAULT_PAGESIZE, startIndex, "sendTime");
		mv.addObject("page", page);
		mv.addObject("messageBean", messageBean);
		mv.setViewName("/admin/message/list");
		return mv;
	}
	
	@RequestMapping("/reSend/{messageId}")
	@ResponseBody
	public Map<String, Object> reSend(@PathVariable Integer messageId,HttpServletRequest request) throws Exception{
		Map<String, Object> model = new HashMap<String, Object>();
		Message message = messageService.findById(messageId);
		if(message != null){
			Message m = new Message();
			m.setName(message.getName());
			m.setPhone(message.getPhone());
			m.setContext(message.getContext());
			m.setRemark(SendMessageUtils.send(message.getPhone(), message.getContext()));
			m.setSendUserName((String)request.getSession().getAttribute(Constants.ONLINE_USERNAME));
			m.setSendTime(new Date());
			messageService.save(m);
			model.put("msg", "succ");
		}else{
			model.put("msg", "error");
		}
		return model;
	}
}
